<?php
namespace App\Module\Admin\Service;

use App\Model\Works;
use App\Module\Admin\Request\Works\CreateRequest;
use App\Module\Admin\Request\Works\UpdateRequest;

class WorksService {

    public function search($child_name,$pagesize){
        $query  = Works::query()->latest("works_id");

        if(!empty($child_name)){
            $query->where("child_name",'like','%' . $child_name . '%');
        }

        return $query->paginate($pagesize);
    }

    public function create(CreateRequest $request){
        $works = new Works();
        $works->fill($request->all());
        $works->save();
    }

    public function getById($id){
        return Works::query()->find($id);
    }

    public function update(UpdateRequest $request,$id){
        $works = Works::query()->findOrFail($id);
        $works->fill($request->all());
        $works->save();
    }

    public function delete($id){
        return Works::destroy($id);
    }
}